package com.valeandre.euroro.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.ClipData.Item;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.valeandre.euroro.R;
import com.valeandre.euroro.model.Currency;
import com.valeandre.euroro.model.Price;
import com.valeandre.euroro.utils.Utils;

public class MaterialsAdapter extends BaseAdapter implements ListAdapter {

	private LayoutInflater mInflater;
	private Currency mCurrency;
	protected HashMap<String, Bitmap> mImageCache = new HashMap<String, Bitmap>();
	protected Handler mHandler = new Handler();
	private DecimalFormat mDf = new DecimalFormat("#.##");

	public MaterialsAdapter(Currency currency, Context context) {
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mCurrency = currency;
	}

	public void setData(Currency currency) {
//		normalizeFeeds(feeds);
		mCurrency = currency;
		notifyDataSetChanged();
	}

	public int getCount() {
		return mCurrency.getPrices().size();
	}

	public Price getItem(int pos) {
		return mCurrency.getPrices().get(pos);
	}

	public long getItemId(int pos) {
		return pos;
	}
	
	public View getView(int position, View convertView, ViewGroup adapterView) {
		View view = convertView;
		if (view == null) {
			view = mInflater.inflate(R.layout.material_item, null);
		}
		Price item = getItem(position);

		TextView material = (TextView) view.findViewById(R.id.material_name);
		TextView value = (TextView) view.findViewById(R.id.material_value);
		TextView currency = (TextView) view.findViewById(R.id.material_currency);
		
		currency.setText(mCurrency.getAccess());
		
		int materialTextResId = Utils.MATERIAL_NAME_RES_MAP.get(item.getAccess());
		material.setText(materialTextResId);
		value.setText(mDf.format(item.getValue()));
		
		ImageView materialImage = (ImageView) view.findViewById(R.id.material_image);
		int imageResId = Utils.MATERIAL_IMAGE_RES_MAP.get(item.getAccess());
		materialImage.setImageResource(imageResId);

		return view;
	}


	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public int getViewTypeCount() {
		return 1;
	}

	public void recycle() {
		for (Bitmap bitmap : mImageCache.values()) {
			if (bitmap != null && !bitmap.isRecycled()) {
				bitmap.recycle();
			}
		}

		mImageCache.clear();
	}
	
	public void reset() {
		recycle();
	}
	
//	private void normalizeFeeds(ArrayList<Item> feeds) {
//		if(feeds != null && feeds.size()>0) {
//			for(Item item: feeds) {
//				item.normalize();
//			}
//		}
//	}
	
	@Override
	public boolean isEnabled(int position) {
		return false;
	}
	
	

}
